package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JComponent;


//Reference: http://www.exampledepot.com/egs/java.awt/Pie.html
public class PieChart extends JComponent {
	    public PieValue[] slices = new PieValue[13];

	    PieChart() {
	        slices[0] = new PieValue(0, new Color(255,111,245));
	        slices[1] = new PieValue(0, new Color(255,0,0));
	        slices[2] = new PieValue(0, new Color(255,192,0));
	        slices[3] = new PieValue(0, new Color(255,255,0));
	        slices[4] = new PieValue(0, new Color(146,208,80));
	        slices[5] = new PieValue(0, new Color(0,176,80));
	        slices[6] = new PieValue(0, new Color(45,205,227));
	        slices[7] = new PieValue(0, new Color(0,112,192));
	        slices[8] = new PieValue(0, new Color(0,32,96));
	        slices[9] = new PieValue(0, new Color(112,48,160));
	        slices[10] = new PieValue(0, new Color(242,242,242));
	        slices[11] = new PieValue(0, new Color(127,127,127));
	        slices[12] = new PieValue(0, new Color(0,0,0));
	    }

	    // This method is called whenever the contents needs to be painted
	    public void paint(Graphics g) {
	        // Draw the pie
	        drawPie((Graphics2D)g, getBounds(), slices);
	    }
	    
		//Class to hold a value for a slice
		public class PieValue {
		    public double value;
		    Color color;

		    public PieValue(double value, Color color) {
		        this.value = value;
		        this.color = color;
		    }
		}
		
		// slices is an array of values that represent the size of each slice.
		public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
		    // Get total value of all slices
		    double total = 0.0D;
		    for (int i=0; i<slices.length; i++) {
		        total += slices[i].value;
		    }

		    // Draw each pie slice
		    double curValue = 0.0D;
		    int startAngle = 0;
		    for (int i=0; i<slices.length; i++) {
		        // Compute the start and stop angles
		        startAngle = (int)(curValue * 360 / total);
		        int arcAngle = (int)(slices[i].value * 360 / total);

		        // Ensure that rounding errors do not leave a gap between the first and last slice
		        if (i == slices.length-1) {
		            arcAngle = 360 - startAngle;
		        }

		        // Set the color and draw a filled arc
		        g.setColor(slices[i].color);
		        g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);

		        curValue += slices[i].value;
		    }
		}
}



